<template>
  <div class='cus_vtree_wrap c-pl20 c-pr40' @click.capture='clickNodeWrap'>
    <q-Tree :treeData='treeData' @clickNodeCom='clickNode' :checkBox='checkBox':class='{tree_root_lonely:treeData.length === 1}'></q-Tree>
  </div>
</template>

<script>

import qTree from "@/components/templates/qTree.vue";

export default {
  name: 'questionTree',
  components: {
    qTree
  },
  props: ['treeData', 'checkBox'],
  data() {
    return {
    }
  },
  mounted() {
    this.initData(true, true);
  },
  updated() {
    this.initData(true, true);
  },
  methods: {
    initData(expandInit, checkedInit) {
      var self = this;
      var modifyDataFun = function (datas) {
        if (datas) {
          datas.forEach((m, index) => {
            if (expandInit) {
              self.$set(m, 'expand', true);
            }
            if (checkedInit) {
              if (m.isCheckd == 1) {
                m.isCheckd = true
              } else {
                m.isCheckd = false
              }
              self.$set(m, 'isCheckd', m.isCheckd);
// self.$set(m, 'partchecked', m.checked);
            }
            if (index === datas.length - 1) {
              self.$set(m, 'last', true);
            }
            if (m.children) {
              modifyDataFun(m.children);
            }
          });
        }
      }
      modifyDataFun(this.treeData);
    },
    clickNode(data) {
      this.$emit('clickNode', data);
    },
    clickNodeWrap() {
      this.initData(false, false);
    },
    getCheckedNodes() {
      let resuldata = {
        resulIdtArr: [],
        resulNametArr: [],
        ChangeDatas: []
      }
      var getCheckedNodesFun = (datas) => {
        if (datas) {
          datas.forEach((m) => {
            if (m.isCheckd && !m.children) {
              resuldata.resulIdtArr.push(m.libraryId);
              resuldata.resulNametArr.push(m.name)
            }
            if (m.children) {
              getCheckedNodesFun(m.children);
            }
          })
          resuldata.ChangeDatas = datas
        }
      };

      getCheckedNodesFun(this.treeData);
      return resuldata;
    },
  }
}
</script>
